﻿using System;
using System.Collections.Generic;
using System.IO;

namespace UMLDesigner
{
    
    public class ClassData
    {
        private string name;
        private string modifier;
        private string visibility;
        private List<AttributeData> attributes = new List<AttributeData>();
        private List<MethodData> methods = new List<MethodData>();
        

        public ClassData()
        {
        }

        public string GetName()
        {
            return name;
        }

        public void SetName(string newName)
        {
            name = newName;
        }

        public string GetVisibility()
        {
            return visibility;
        }

        public void SetVisibility(string newVisibility)
        {
            visibility = newVisibility;
        }

        public string GetModifier()
        {
            return modifier;
        }

        public void SetModifier(string newModifier)
        {
            modifier = newModifier;
        }

        public List<AttributeData> GetAttributes()
        {
            return attributes;
        }

        public void SetAttributes(List<AttributeData> newAttributes)
        {
            attributes.Clear();
            for (int i = 0; i < newAttributes.Count; i++)
            {
                attributes.Add(newAttributes[i]);
            }
        }

        public List<MethodData> GetMethods()
        {
            return methods;
        }

        public void SetMethods(List<MethodData> newMethods)
        {
            methods.Clear();
            for (int i = 0; i < newMethods.Count; i++)
            {
                methods.Add(newMethods[i]);
            }
           
        }

        public void AddAttribute(string name, string type, string visibility)
        {
            AttributeData nuevoAtributo = new AttributeData(name, type, visibility);
            attributes.Add(nuevoAtributo);
        }

       
        public void ExportCode(StreamWriter fileW, String projectName)
        {
            List<RelationData> relations = WindowsManager.GetRelationOfClass(this);

            fileW.WriteLine("using System;");
            fileW.WriteLine("using System.Collections.Generic;");

            fileW.WriteLine("namespace " + projectName);
            fileW.WriteLine("{");

            string parentClass = "";
            foreach (RelationData rel in relations)
            {
                if (rel.GetRelationType() == RelationData.RelationType.Inheritance)
                {
                    parentClass = " : " + rel.GetEndClass().GetName();
                }
            }

            if (modifier == "none")
            {
                modifier = "";
            }
            fileW.WriteLine(visibility + " "+modifier+" class " + name + parentClass);
            fileW.WriteLine("{");

            foreach (AttributeData attr in attributes)
            {
                fileW.WriteLine(attr.GetVisibility() + " " + attr.GetAttrType() + " " + attr.GetName() + ";");
            }
            
            foreach (RelationData rel in relations)
            {
                if (rel.GetRelationType() != RelationData.RelationType.Inheritance)
                {
                    if (rel.GetIniCardinality() == "1")
                    {
                        fileW.WriteLine(rel.GetVisibilityIniAttribute() + " " + rel.GetIniClass().GetName() + " " + rel.GetAttrNameIni() + ";");
                    }
                    else
                    {
                        int positionMaxCardinality = rel.GetIniCardinality().LastIndexOf('.');
                        string maxCardinality = rel.GetIniCardinality().Substring(positionMaxCardinality + 1);
                        if (maxCardinality != "*")
                        {
                            fileW.WriteLine(rel.GetVisibilityIniAttribute()+" "+rel.GetIniClass().GetName() + "[]" + " " + rel.GetAttrNameIni() + " = " + "new " + rel.GetIniClass().GetName() + "[" + maxCardinality + "];");
                        }
                        else
                        {
                            fileW.WriteLine(rel.GetVisibilityIniAttribute() + " " + "List<" + rel.GetIniClass().GetName() + "> " + rel.GetAttrNameIni() + " = new List<" + rel.GetIniClass().GetName() + ">();");
                        }
                    }
                }
            }

            //Default constructor
            fileW.WriteLine("public " + name + "()");
            fileW.WriteLine("{");
            fileW.WriteLine("}");

            foreach (MethodData met in methods)
            {
                fileW.Write(met.GetVisibility() + " " + met.GetReturnType() + " " + met.GetName() + " ( ");
                for (int i = 0; i < met.GetParameters().Count; i++)
                {
                    fileW.Write(met.GetParameters()[i].GetParamType() + " " + met.GetParameters()[i].GetName());
                    if (i != met.GetParameters().Count - 1)
                    {
                        fileW.Write(", ");
                    }
                }
                fileW.WriteLine(" )");
                fileW.WriteLine("{\n");
                if(met.GetReturnType()!="void"){
                    fileW.WriteLine("return " + met.GetReturnType()+";");
                }
                fileW.WriteLine("}\n");
            }

            fileW.WriteLine("}");  //Class end
            fileW.WriteLine("}");  //Namespace end
        }
    }
}
